/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var erroruser = true;
var errorpass = true;
var errorname = true;
var erroremail = true;
var erroradd = true;
var errorphone = true;
var errorconfirmpass = true;
var listuser = new Array();
var listemail = new Array();
/* check min length fullname*/
function minLengName(text){
    if(text.length==0){
        return true;
    }
    return false;
}
/* check max length fullname*/
function maxLengName(text){
    if(text.length>30){
        return true;
    }
    return false;
}
/* check max length email*/
function maxLengEmail(text){
    if(text.length>100){
        return true;
    }
    return false;
}
/* check max length address*/
function maxLengAddress(address){
    if(address.length>200){
        return true;
    }
    return false;
}
/* check leng of user*/
function lengUser(text){
    if(text.length>=4){
        if(text.length<=15){
            return true;
        }
    }
    return false;
}
/* check leng of password*/
function lengPass(text){
    if(text.length>=4){
        if(text.length<=30){
            return true;
        }
    }
    return false;
}
/* check existed user */
function existUser(text){
    if(listuser){
        for(var i=0;i<listuser.length;i++){
            if(text==listuser[i]){
                return true;
            }
        } 
    }
    return false;
}
/*check existed email*/
function existEmail(email){
    if(listemail){
        for(var i=0;i<listemail.length;i++){
            if(email==listemail[i]){
                return true;
            }
        }
    }
    return false;
}
/*check form email*/
function checkFormEmail(email){
    var rule = /^([\w\-\.])*\w+@(\w+\.)+[A-Za-z]+$/;
    if(rule.test(email)){
        return false;
    }
    return true;
}
/* check phonenumber */
function checkPhone(phone){
    var rule = /^\d{9,15}$/;
    if(rule.test(phone)){
        return false;
    }
    return true;
}
/* check special character */
function SpecialChar(text){
    var rule = /^[A-Za-z\d]*$/;
    if(rule.test(text)){
        return true;
    }
    return false;
}
function onDisplay(text){
    if(document.getElementById(text).style.display=="none"){
        document.getElementById(text).style.display="block";
    } 
}
function offDisplay(text){
    if(document.getElementById(text).style.display=="block") {
        document.getElementById(text).style.display="none";
    }
}
/* check validate of username */
function validUser(user, errorLeng, errorSpec, errorExist){
    if(!lengUser(user)){
        onDisplay(errorLeng);
        offDisplay(errorSpec);
        offDisplay(errorExist);
    } else{
        offDisplay(errorLeng);
        if(!SpecialChar(user)){
            onDisplay(errorSpec);
            offDisplay(errorExist);
        } else{
            offDisplay(errorSpec);
            if(existUser(user)){
                onDisplay(errorExist);
            } else{
                offDisplay(errorExist);
                erroruser = false;
            }
        }
    }
}
/*check validate fullname */
function validFullName(name, errorMinLeng, errorMaxLeng){
    if(minLengName(name)){
        onDisplay(errorMinLeng);
        offDisplay(errorMaxLeng);
    } else {
        offDisplay(errorMinLeng);
        if(maxLengName(name)){
            onDisplay(errorMaxLeng);
        } else{
            offDisplay(errorMaxLeng);
            errorname = false;
        }
    }
}
/* check validate password*/
function validPass(pass, error){
    if(!lengPass(pass)){
        onDisplay(error);
    } else{
        offDisplay(error);
        errorpass = false;
    }
}
/* check validate email */
function validEmail(email, errorMinLeng, errorMaxLeng, errorExist){
    if(minLengName(email)){
        onDisplay(errorMinLeng);
        offDisplay(errorMaxLeng);
        offDisplay(errorExist);
    } else {
        offDisplay(errorMinLeng);
        if(maxLengEmail(email)||checkFormEmail(email)){
            onDisplay(errorMaxLeng);
            offDisplay(errorExist);
        } else{
            offDisplay(errorMaxLeng);
            if(existEmail(email)){
                onDisplay(errorExist);
            } else{
                offDisplay(errorExist);
                erroremail=false;
            }
        }
    }
}
/* validate address*/
function validAdd(address, errorMinLeng, errorMaxLeng){
    if(minLengName(address)){
        onDisplay(errorMinLeng);
        offDisplay(errorMaxLeng);
    } else{
        offDisplay(errorMinLeng);
        if(maxLengAddress(address)){
            onDisplay(errorMaxLeng);
        } else{
            offDisplay(errorMaxLeng);
            erroradd = false;
        }
    }
}
/* validate phone number */
function validPhone(phone, error){
    if(checkPhone(phone)){
        onDisplay(error);
    } else{
        offDisplay(error);
        errorphone=false;
    }
}
/* not allow submit when an error occur */
function disableSubmit(){
    if(!erroruser){
        if(!errorpass){
            if(!errorname){
                if(!erroremail){
                    if(!erroradd){
                        if(!errorphone){
                            if(!errorconfirmpass)
                            return true;
                        }
                    }
                }
            }
        }
    }
    alert("L\u1ed7i đăng ký....\n\nVui lòng nhập chính xác và đầy đủ thông tin!!");
    return false;
}
